package com.tgy.稀疏数组;

import java.io.*;
import java.util.Arrays;

/***
 * @ClassName: Sparsearray
 * @Description: 稀疏数组
 * @Auther: tanggy3
 * @Date: 10:27 2022/5/17
 * @version : V1.0
 */
public class Sparsearray {
    public static void main(String[] args) {
        //创建棋盘
        int[][] intArray = new int[11][11];
        //初始化棋盘
        intArray[1][2] = 1;
        intArray[2][3] = 2;
        printArray(intArray);
        System.out.println(saveFile("aaa.data", intArray));
        int[][] ints = readFile("aaa.data");
        printArray(ints);
        int[][] compress = compress(intArray);
        printArray(compress);
        int[][] reduction = reduction(compress);
        printArray(reduction);

    }

    /**
     * @Param
     * @Description: 压缩二维数组
     * @auther: Tanggy3
     * @date: 10:43 2022/5/17
     * @return:
     */
    public static int[][] compress(int[][] intArray) {
        //遍历原始数组得到有效个数
        int count = 0;
        for (int i = 0; i < intArray.length; i++) {
            for (int j = 0; j < intArray[i].length; j++) {
                if (intArray[i][j] != 0) {
                    count++;
                }
            }
        }
        //初始化压缩数组
        int[][] compressArray = new int[count+1][3];
        Arrays.asList(compressArray).forEach(row -> Arrays.fill(row, 0));
        //遍历得到非零数据并填充到压缩数组
        compressArray[0][0] = intArray.length;
        compressArray[0][1] = intArray[0].length;
        compressArray[0][2] = count;
        int index = 1;
        //使用标签跳出多层嵌套循环
        outCycle:
        for (int i = 0; i < intArray.length; i++) {
            for (int j = 0; j < intArray[i].length; j++) {
                if (intArray[i][j] != 0) {
                    compressArray[index][0] = i;
                    compressArray[index][1] = j;
                    compressArray[index][2] = intArray[i][j];
                    if (index++ == count+1) break outCycle;
                }
            }
        }
        return compressArray;
    }

    /**
     * @Param
     * @Description: 还原压缩数组
     * @auther: Tanggy3
     * @date: 11:07 2022/5/17
     * @return:
     */
    public static int[][] reduction(int[][] compressArray){
        int[][] intArray = new int[compressArray[0][0]][compressArray[0][1]];
        //遍历压缩数组，还原数据
        for (int i = 1 ; i < compressArray.length; i++) {
            intArray[compressArray[i][0]][compressArray[i][1]] = compressArray[i][2];
        }
        return intArray;
    }


    /**
     * @Param
     * @Description: 打印二维数组
     * @auther: Tanggy3
     * @date: 10:49 2022/5/17
     * @return:
     */
    public static void printArray(int[][] intArray){
        System.out.println("==================开始打印数组=====================");
        Arrays.stream(intArray).forEach(row -> System.out.println(Arrays.toString(row)));
    }

    /**
     * @Param
     * @Description: 将二维数组保存成文件
     * @auther: Tanggy3
     * @date: 11:12 2022/5/17
     * @return:
     */
    public static boolean saveFile(String fileName,int[][] intArray){
        boolean flag = false;
        FileOutputStream out = null;
        try {
            File file = new File(fileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            out = new FileOutputStream(file);
            ObjectOutputStream objOut = new ObjectOutputStream(out);
            objOut.writeObject(intArray);
            objOut.flush();
            objOut.close();
            flag = true;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @Param
     * @Description: 读取文件
     * @auther: Tanggy3
     * @date: 14:12 2022/5/17
     * @return:
     */
    public static int[][] readFile(String fileName){
        int[][] intArray = null;

        try {
            FileInputStream fileInputStream = new FileInputStream(fileName);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            intArray = (int[][]) objectInputStream.readObject();
            objectInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return intArray;
    }
}
